package com.bfly.mall.product.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.admin.entity.Admin;
import com.bfly.mall.order.entity.OrderSku;
import com.bfly.mall.order.service.IOrderSkuService;
import com.bfly.mall.product.dao.ProductCommentMapper;
import com.bfly.mall.product.dto.ProductCommentFormDTO;
import com.bfly.mall.product.entity.ProductComment;
import com.bfly.mall.product.enums.ProductCommentStatusEnum;
import com.bfly.mall.product.service.IProductCommentService;
import com.bfly.mall.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/10/4 9:41
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class ProductCommentServiceImpl extends BaseServiceImpl<ProductComment, Integer> implements IProductCommentService {

    @Autowired
    private ProductCommentMapper productCommentMapper;
    @Autowired
    private IOrderSkuService skuService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void save(User user, ProductCommentFormDTO formDTO) {
        OrderSku orderSku = skuService.getOrderSku(formDTO.getOrderNo(), formDTO.getSn());
        if (orderSku == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的订单信息!");
        }
        if (orderSku.isEvaluate()) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不能重复评价!");
        }
        ProductComment comment = new ProductComment();
        comment.setOrderNo(formDTO.getOrderNo());
        comment.setSn(formDTO.getSn());
        comment.setProductId(orderSku.getProductId());
        comment.setCreateDate(new Date());
        comment.setReply(false);
        comment.setPoster(true);
        comment.setStatus(ProductCommentStatusEnum.WAIT.getId());
        comment.setContent(formDTO.getContent());
        comment.setNickName(user.getNickName());
        comment.setUserId(user.getId());
        comment.setAvatarUrl(user.getAvatarUrl());
        comment.setParentId(0);

        comment.setSameScore(formDTO.getSameScore());
        comment.setServiceScore(formDTO.getServiceScore());
        comment.setShipScore(formDTO.getShipScore());

        float score = (formDTO.getSameScore() + formDTO.getServiceScore() + formDTO.getShipScore()) / 3;    //三者平均值
        BigDecimal decimal = new BigDecimal(score);
        score = decimal.setScale(1, BigDecimal.ROUND_DOWN).intValue();
        comment.setScore(score);
        super.save(comment);

        // 修改已评价
        orderSku.setEvaluate(true);
        skuService.edit(orderSku);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reply(Admin admin, ProductCommentFormDTO formDTO) {
        ProductComment comment = get(formDTO.getParentId());
        if (comment == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "评论信息不存在!");
        }
        ProductComment replyComment = new ProductComment();
        replyComment.setNickName(admin.getUserName());
        replyComment.setAvatarUrl(admin.getAvatarUrl());
        replyComment.setUserId(admin.getId());

        replyComment.setParentId(formDTO.getParentId());
        replyComment.setContent(formDTO.getContent());

        replyComment.setOrderNo(comment.getOrderNo());
        replyComment.setPoster(false);
        replyComment.setReply(true);
        replyComment.setScore(0);
        // 管理员回复直接审核通过
        replyComment.setStatus(ProductCommentStatusEnum.PASS.getId());
        replyComment.setSn(comment.getSn());
        replyComment.setProductId(comment.getProductId());

        replyComment.setCreateDate(new Date());
        replyComment.setUpdateDate(replyComment.getCreateDate());
        super.save(replyComment);

        // 设置原评论为已回复状态
        comment.setReply(true);
        comment.setUpdateDate(replyComment.getCreateDate());
        super.edit(comment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editCommentStatus(int commentId, ProductCommentStatusEnum status) {
        ProductComment comment = get(commentId);
        if (comment == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "评论信息不存在!");
        }
        if (status == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "状态参数错误!");
        }

        comment.setStatus(status.getId());
        super.edit(comment);
    }

    @Override
    public List<ProductComment> getComments(Integer productId, ProductCommentStatusEnum statusEnum) {
        return productCommentMapper.getComments(productId, statusEnum == null ? null : statusEnum.getId());
    }
}
